package com.xxx.controller;

import com.xxx.controller.utils.R;
import com.xxx.service.CartService;
import com.xxx.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController extends BaseController {
    @Autowired
    private CartService cartService;

    //添加商品到购物车功能
    @RequestMapping("/addToCart")
    public R addToCart(Integer pid,Integer amount,HttpSession session){
        System.out.println("=====>"+pid);
        System.out.println("=====>"+amount);
        cartService.addToCart(getUidFromSession(session),pid,amount,getUsernameFromSession(session));
        return new R(OK,"加入购物车成功");
    }

    //显示购物车商品数据功能
    @RequestMapping("/getCartProducts")
    public R getCartProducts(HttpSession session){
        List<CartVo> cartVoList = cartService.getCartProducts(getUidFromSession(session));
        return new R(OK,cartVoList);
    }

    //修改购物车内商品数量功能  option 代表 增加 还是 减少 商品数量 操作
    @RequestMapping("/changeNum")
    public R changeNum(Integer cid,Integer amount,String option,HttpSession session){
        System.out.println();
        int num = cartService.changeNum(cid, getUsernameFromSession(session), amount,option);
        return new R(OK,num);
    }

    //批量删除购物车数据功能
    @RequestMapping("/deleteProducts")
    public R deleteProducts(String cidStr){
        String[] splitResult = cidStr.split(",");
        Integer[] cids=new Integer[splitResult.length];
        for (int i=0;i<splitResult.length;i++){
            cids[i]=Integer.parseInt(splitResult[i]);
        }
        cartService.deleteProducts(cids);
        return new R(OK);
    }

    //通过cid获取购物车所有商品总价功能
    @RequestMapping("/getTotalPriceByCids")
    public  R getTotalPriceByCids(String cidStr){
        String[] splitResult = cidStr.split(",");
        Integer[] cids=new Integer[splitResult.length];
        for (int i=0;i<splitResult.length;i++){
            cids[i]=Integer.parseInt(splitResult[i]);
        }
        Float totalPriceByCids = cartService.getTotalPriceByCids(cids);
        return new R(OK,totalPriceByCids);
    }

    //通过cids获取订单页面要显示的数据 功能
    @RequestMapping("/getOrderList")
    public R getOrderList(Integer[] ck ){
        List<CartVo> orderList = cartService.getOrderList(ck);
        return new R(OK,orderList);
    }
}
